package com.yf.modules.system.controller;

import com.yf.Utils.PageUtil;
import com.yf.domain.security.Menu;
import com.yf.modules.security.model.dto.MenuDto;
import com.yf.modules.security.utils.SecurityContext;
import com.yf.model.query.MenuQueryObject;
import com.yf.modules.system.model.vo.MenuVo;
import com.yf.modules.system.service.api.MenuService;
import com.yf.response.ResponseBody;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author chentianwei
 * @date 2023/5/23 16:39
 * @description menuRestful
 */
@RestController
@RequestMapping("api/menus")
@Slf4j
@Tag(name = "MenuApi", description = "菜单Api")
public class MenuController {

    private final MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    @GetMapping("")
    @Operation(summary = "获取菜单", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<Object> getMenus(MenuQueryObject menuQueryObject) {
        List<MenuDto> result = menuService.queryMenu(menuQueryObject);
        return ResponseEntity.ok().body(ResponseBody.success("操作成功", PageUtil.toPage(result, result.size())));
    }

    @GetMapping("/lazy")
    @Operation(summary = "懒加载子菜单", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<Object> getMenusTree(@RequestParam Long pid) {
        List<Menu> menusTrees = menuService.getMenusTrees(pid);
        return ResponseEntity.ok().body(ResponseBody.success("操作成功", menusTrees));
    }

    @PostMapping("/superior")
    @Operation(summary = "根据ID获取上级数据【编辑菜单】")
    public ResponseEntity<Object> getMenusSuperior(@RequestBody List<Long> ids) {
        List<?> superior = menuService.getMenuSuperior(ids);
        return ResponseEntity.ok().body(ResponseBody.success("操作成功", superior));
    }

    @GetMapping("/build")
    @Operation(summary = "构建菜单", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<Object> buildMenus() {
        List<MenuDto> menuDtoList = menuService.findByUser(SecurityContext.getCurrentUserId());
        List<MenuDto> menuDtos = menuService.buildTrees(menuDtoList);
        List<MenuVo> menuVos = menuService.buildMenus(menuDtos);
        return ResponseEntity.ok().body(ResponseBody.success("操作成功", menuVos));
    }

    @PostMapping("/addMenu")
    @Operation(summary = "添加菜单", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<Object> addMenu(@RequestBody Menu resource) {
        menuService.addMenu(resource);
        return ResponseEntity.ok().body(ResponseBody.success("添加成功"));
    }

    @PutMapping("/updateMenu")
    @Operation(summary = "更新菜单", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<Object> updateMenu(@RequestBody Menu resource) {
        menuService.update(resource);
        return ResponseEntity.ok().body(ResponseBody.success("更新成功"));
    }

    @DeleteMapping("/deleteMenu")
    @Operation(summary = "删除菜单", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<Object> deleteMenu(@RequestBody Set<Long> ids) {
        menuService.delete(ids);
        return ResponseEntity.ok().body(ResponseBody.success("删除成功"));
    }

    @GetMapping("/download")
    @Operation(summary = "导出菜单数据", security = @SecurityRequirement(name = "authScheme"))
    public void exportMenu(HttpServletResponse response, MenuQueryObject queryObject) {
        menuService.download(response, queryObject);
    }

    @GetMapping(value = "/child")
    @Operation(summary = "获取菜单ID下所有的子节点ID集合",security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<Object>> getMenuChildIds(@RequestParam Long id){
        Set<Long> childIds = menuService.getMenuChildIds(id);
        return ResponseEntity.ok().body(ResponseBody.success(childIds));
    }

}
